package com.tgit.sso.paltform.config;

import com.tgit.sso.core.conf.SsoConf;
import com.tgit.sso.core.conf.SsoConfigurationStore;
import com.tgit.sso.core.util.JedisUtil;
import com.tgit.sso.paltform.util.StringUtil;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import java.util.Properties;
import java.util.Set;

/**
 * ClassName: ConfigInfo
 * date: 2019/11/22 15:24
 * Description: 配置信息
 * @author fanzj@tgit.com
 * @since JDK 1.8
 */
@Configuration("ConfigInfo")
public class ConfigInfo implements InitializingBean {


    @Autowired
    private SsoConfigBean ssoConfigBean;

    /**************************************************db连接信息********************************************************/
    public static final Properties PROPERTY_MAP = new Properties();

    /*************************************************common节点**********************************************/
    //是否国际化
    public static Boolean LANGUAGE_FLAG;

    //redis集群配置 golbal.xml-redis
    public static Set<String> REDIS_CLUSTER;
    //redis密码
    public static String REDIS_PASSWORD;

    /**************************************************公司相关信息********************************************************/
    //公司版权信息 golbal.xml-conpany
    public static String COPYRIGHT;
    //公司全称 golbal.xml-conpany
    public static String COMPANY_FULL_NAME;
    public static String COMPANY_FULL_NAME_US;

    /**********************************************************************************************/
    //白名单IP地址
    public static Set<String> WHITE_LIST;

    //设置session过期时间
    public static Integer DEFAULT_SESSION_TIMEOUT;

    //sso关联的产品
    //sso
    public static String CAS_URL;
    //融合产品
    public static String DEFAULT_URL;

    //sso公告
    //内容
    public static String NOTICE_CONTENT;
    public static String NOTICE_CONTENT_EN;
    //开始时间 格式：yyyy-MM-dd HH:mm:ss
    public static String NOTICE_START_TIME;
    //结束时间 格式：yyyy-MM-dd HH:mm:ss
    public static String NOTICE_END_TIME;
    //显示标志: 0-隐藏 1-显示
    public static Integer NOTICE_FLAG;

    @Override
    public void afterPropertiesSet() throws Exception {
        init();
    }


    private void init() {
        readConfigInfo(ssoConfigBean);
        //sso-redis配置
        JedisUtil.init(StringUtil.setToString(ConfigInfo.REDIS_CLUSTER), ConfigInfo.REDIS_PASSWORD);

        //删除redis旧的配置项和键值对
        JedisUtil.batchDel(SsoConf.SSO_CONFIGURATION);
        JedisUtil.batchDel(SsoConf.SSO_SESSIONID);
        //配置项信息存入redis，供客户端ssofilter使用
  /*      if(imax.getDomain() != null) {
            SsoConfigurationStore.put(SsoConf.IMAX_URL, imax.getDomain());
        }*/
        if(ConfigInfo.DEFAULT_SESSION_TIMEOUT > 0) {
            SsoConfigurationStore.put(SsoConf.SESSION_TIMEOUT, String.valueOf(ConfigInfo.DEFAULT_SESSION_TIMEOUT));
        }

    }

    /**
     * 读取本地配置
     * @param ssoConfigBean
     */
    private void readConfigInfo(SsoConfigBean ssoConfigBean) {
        this.PROPERTY_MAP.put("driver.class", ssoConfigBean.getDbDriverClassName());
        this.PROPERTY_MAP.put("url", ssoConfigBean.getDbUrl());
        this.PROPERTY_MAP.put("username", ssoConfigBean.getDbUsername());
        this.PROPERTY_MAP.put("password", ssoConfigBean.getDbPassword());

        this.REDIS_CLUSTER=StringUtil.stringToSet(ssoConfigBean.getRedisAddress(), ",");
        this.REDIS_PASSWORD=ssoConfigBean.getRedisPassword();

        this.LANGUAGE_FLAG = ssoConfigBean.getLanguageFlag();
        if(this.LANGUAGE_FLAG == null) {
            this.LANGUAGE_FLAG = true;
        }

        this.COPYRIGHT = ssoConfigBean.getCopyright();
        this.COMPANY_FULL_NAME = ssoConfigBean.getCompanyFullName();
        this.COMPANY_FULL_NAME_US = ssoConfigBean.getCompanyFullNameUs();

        this.DEFAULT_SESSION_TIMEOUT = ssoConfigBean.getSessionTimeout();
        if(this.DEFAULT_SESSION_TIMEOUT == null || ConfigInfo.DEFAULT_SESSION_TIMEOUT < SsoConf.SESSION_TIMEOUT_RANGE1
                || this.DEFAULT_SESSION_TIMEOUT > SsoConf.SESSION_TIMEOUT_RANGE2) {
            this.DEFAULT_SESSION_TIMEOUT = -1;
        }

        this.WHITE_LIST=StringUtil.stringToSet(ssoConfigBean.getWhiteIpList(), ",");

        this.CAS_URL = ssoConfigBean.getCasUrl();
        this.DEFAULT_URL = ssoConfigBean.getDefaultUrl();

        this.NOTICE_CONTENT = ssoConfigBean.getNoticeContent();
        this.NOTICE_CONTENT_EN = ssoConfigBean.getNoticeContentEn();
        this.NOTICE_START_TIME = ssoConfigBean.getNoticeStartTime();
        this.NOTICE_END_TIME = ssoConfigBean.getNoticeEndTime();
        this.NOTICE_FLAG = ssoConfigBean.getNoticeFlag();
    }

}
